package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.Entity;
import com.zw.yzk.kabu.data.network.task.AddShippingAddressTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.view.view.AddShippingAddressView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/12.
 */

public class AddShippingAddressPresenter implements Presenter {

    private UseCase addShippingAddress;
    private AppCompatActivity activity;
    private AddShippingAddressView addShippingAddressView;

    @Inject
    public AddShippingAddressPresenter(@Named("addShippingAddressUseCase") UseCase addShippingAddress, AppCompatActivity activity) {
        this.addShippingAddress = addShippingAddress;
        this.activity = activity;
    }

    public void setView(AddShippingAddressView view) {
        this.addShippingAddressView = view;
    }

    public void addShippingAddress(String receiverName, String receiverPhone, String province, String city, String address) {
        addShippingAddress.execute(new AddShippingAddressSubscriber(activity),
                new AddShippingAddressTask(receiverName, receiverPhone, province, city, "", address));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        addShippingAddress.unSubscribe();
    }

    private class AddShippingAddressSubscriber extends BaseSubscriber<Entity> {


        public AddShippingAddressSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(Entity entity) {
            super.onNext(entity);
            ToastManager.getInstance().showToast(activity, activity.getResources().getString(R.string.add_shipping_address_success));
            addShippingAddressView.addAddressSuccess();
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.add_shipping_address_failed);
        }
    }
}
